package secondriver.heathfood.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import secondriver.heathfood.R;
import secondriver.heathfood.bean.FoodClass;

import java.util.List;

/**
 * Author : secondriver
 */
public class FoodClassListAdapter extends BaseAdapter {

    protected final Context context;
    protected final LayoutInflater inflater;
    protected final List<FoodClass> data;

    public FoodClassListAdapter(Context context, List<FoodClass> data) {
        this.context = context;
        this.data = data;
        this.inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return null != data ? data.size() : 0;
    }

    @Override
    public Object getItem(int i) {
        return null != data ? data.get(i) : null;
    }

    @Override
    public long getItemId(int i) {
        FoodClass foodClass = (FoodClass) getItem(i);
        return null != foodClass ? foodClass.getId() : 0;
    }


    @Override
    public int getItemViewType(int position) {
        return super.getItemViewType(position);
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder.FoodClassViewHolder viewHolder;
        if (view == null) {
            view = inflater.inflate(R.layout.list_item_food_class, viewGroup, false);
            viewHolder = new ViewHolder.FoodClassViewHolder(view);
            view.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder.FoodClassViewHolder) view.getTag();
        }
        FoodClass foodClass = data.get(i);
        viewHolder.setContent(foodClass);
        return view;
    }
}
